package com.wx.wechat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wx.commons.JsapiTicketController;
import com.wx.core.vo.JsapiTicket;
import com.wx.entity.SysWxuser;
import com.wx.entity.WxPicReply;
import com.wx.service.SysWxuserService;
import com.wx.service.WxJsapiTicketService;
import com.wx.service.WxPicReplyService;

/**
 * <b>描述：</b>微信图文详情<br/>
 * <b>作者：</b>Bob <br/>
 * <b>修改日期：</b>2016年11月1日 - 下午8:03:08<br/>
 *
 */
@Controller
@RequestMapping("anon/wx/article")
public class MulArticleController extends JsapiTicketController {

	@Resource
	private WxPicReplyService wxPicReplyService;

	@Resource
	private SysWxuserService sysWxuserService;

	@Resource
	private WxJsapiTicketService wxJsapiTicketService;

	@RequestMapping("{id}")
	public String showArticle(@PathVariable Integer id,HttpServletRequest request, HttpServletResponse response,Model model){

		if(id != null){
			WxPicReply bean = this.wxPicReplyService.selectByPrimaryKey(id);
			if(null != bean){
				SysWxuser wxuser = sysWxuserService.selectByWxid(bean.getWxid());
				String summ = bean.getpSummary();
				summ = summ.replaceAll("[\n|\t|\r]","");
				bean.setpSummary(summ);
				// 获取微信账号信息
				model.addAttribute("wxuser",wxuser);
				// 记录阅读次数
				bean.setWatchTimes(bean.getWatchTimes() + 1);
				wxPicReplyService.updateById(bean);
				if(bean.getIsShow() == 1){
					String imgPath = bean.getpImgHref();
					StringBuffer sb = new StringBuffer();
					sb.append("<img style='width:100%;' src='");
					sb.append(getAbsolutelyServerPath(request));
					sb.append(imgPath);
					sb.append("' />");
					sb.append(bean.getDetailContent());
					bean.setDetailContent(sb.toString());
				}
				model.addAttribute("bean",bean);
				JsapiTicket jsapiTicket = getJsapiTicket(wxuser.getWxid(),request);
				model.addAttribute("jsapiTicket",jsapiTicket);
			}
			model.addAttribute("absolutelyPath",getAbsolutelyServerPath(request));
		}
		return "basic/shared/article";
	}

	@Override
	protected WxJsapiTicketService getJsapiTicketService(){

		return this.wxJsapiTicketService;
	}
}
